﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Library
{
	/// <summary>
	/// 李天
	/// </summary>
    public class  CssHelper
    {
        /// <summary>
        /// 注册通用客户端函数
        /// </summary>
        /// <param name="p"></param>
        public static void RegisterCommonJsFunction(Page p)
        {
			string script = Library.WebCommon.SiteRoot + "/JS/Function.js";
            script = @" <script src='" + script + @"' language='javascript'></script>";

            p.ClientScript.RegisterClientScriptBlock(p.GetType(), "@CommonJsFunction", script, false);
        }

        /// <summary>
        /// 设置该页中所有DataGrid的样式

        /// </summary>
        /// <param name="p"></param>
        public static void SetAllGridCss(Page p)
        {
            SetAllGridCss(p, false);
        }

        public static void SetAllGridCss(Page p,bool isEnableCheckBox)
        {
            if (isEnableCheckBox)
            {
                RegisterCommonJsFunction(p);
            }
            //register GridCss Function
            //string script = @"function GridItemHover(){var obj = GetFatherNode(event.srcElement.parentNode,'TR');obj.className='GridHoverItem';};";
            //script += @"function GridItem(){var obj = GetFatherNode(event.srcElement.parentNode,'TR');obj.className='GridItem';};";

            //p.ClientScript.RegisterClientScriptBlock(p.GetType(), "@GridItemCss", script, true);

            //只检查Form下的第一级和第二级子控件
            foreach (Control ctrl in p.Form.Controls)
            {
                if (ctrl is DataGrid)
                {
                    DataGridItemDataBoundEvent(ctrl, isEnableCheckBox);
                }
                if (ctrl is GridView)
                {
                    GridViewItemDataBoundEvent(ctrl, isEnableCheckBox);
                }

                foreach (Control ctrlChild in ctrl.Controls)
                {
                    if (ctrlChild is DataGrid)
                    {
                        DataGridItemDataBoundEvent(ctrlChild, isEnableCheckBox);
                    }
                    if (ctrl is GridView)
                    {
                        GridViewItemDataBoundEvent(ctrlChild, isEnableCheckBox);
                    }
                }

            }

        }

        #region Event

        public static void DataGridItemDataBoundEvent(Control ctrl, bool isEnableCheckBox)
        {

            DataGrid dgrdShow = ctrl as DataGrid;

            if (isEnableCheckBox)
            {
                dgrdShow.ItemDataBound += new DataGridItemEventHandler(TargetDataGridChoose_ItemDataBound);
            }
            else
            {
                dgrdShow.ItemDataBound += new DataGridItemEventHandler(TargetDataGrid_ItemDataBound);
            }

        }

        public static void GridViewItemDataBoundEvent(Control ctrl,bool isEnableCheckBox)
        {
            GridView gdvShow = ctrl as GridView;

            if (isEnableCheckBox)
            {
                gdvShow.RowDataBound += new GridViewRowEventHandler(TargetGridViewChoose_RowDataBound);
            }
            else
            {
                gdvShow.RowDataBound += new GridViewRowEventHandler(TargetGridView_RowDataBound);
            }
        }

        #endregion

        #region GridView
        /// <summary>
        ///  光标移上去的样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void TargetGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onmouseover", "javascript:this.className='GridHoverItem';");

                e.Row.Attributes.Add("onmouseout", "javascript:this.className='GridItem';");
            }
        }

        /// <summary>
        /// 单击某一行,进行选择,前提是CheckBox在第一列

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void TargetGridViewChoose_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            TargetGridView_RowDataBound(sender, e);

            if ( e.Row.RowIndex >= 0)
            {
                e.Row.Attributes["style"] = "cursor:hand";

                if (e.Row.Attributes["onclick"] != null && e.Row.Attributes["onclick"] != string.Empty)
                {
                    e.Row.Attributes["onclick"] = "CheckRow();" + e.Row.Attributes["onclick"];
                }
                else
                {
                    e.Row.Attributes["onclick"] = "CheckRow();";
                }
                //e.Row.Attributes["onclick"] = GetAttributes("CheckRow()",e.Row.Attributes["onclick"]);
            }
        }
        #endregion

        ///// <summary>
        /////  
        ///// </summary>
        ///// <param name="str"></param>
        ///// <param name="strAttribute"></param>
        ///// <returns></returns>
        //private static string GetAttributes(string str,string strAttribute)
        //{
        //    if (strAttribute != null && strAttribute != string.Empty)
        //    {
        //        strAttribute = str + ";" + strAttribute;
        //    }
        //    else
        //    {
        //        strAttribute = str + ";";
        //    }

        //    return str;
        //}


        #region DataGrid
        /// <summary>
        /// 单击某一行,进行选择,前提是CheckBox在第一列

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void TargetDataGridChoose_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            TargetDataGrid_ItemDataBound(sender, e);

            if (e.Item.ItemIndex >= 0)
            {
                e.Item.Attributes["style"] = "cursor:hand";

                if (e.Item.Attributes["onclick"] != null && e.Item.Attributes["onclick"] != string.Empty)
                {
                    e.Item.Attributes["onclick"] = "CheckRow();" + e.Item.Attributes["onclick"];
                }
                else
                {
                    e.Item.Attributes["onclick"] = "CheckRow();";
                }
            }
        }


        /// <summary>
        ///  光标移上去的样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void TargetDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {

            if (e.Item.ItemIndex >= 0)
            {
                //样式设置
               // e.Item.Attributes["style"] = "cursor:hand";

                //e.Item.Attributes.Add("onmouseover", "javascript:GridItemHover();");

                //e.Item.Attributes.Add("onmouseout", "javascript:GridItem();");

                e.Item.Attributes.Add("onmouseover", "javascript:this.className='GridHoverItem';");

                e.Item.Attributes.Add("onmouseout", "javascript:this.className='GridItem';");
            }
        }

        #endregion

    }
}
